// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#include "PositionRange.h"

CangjieMigrator::PositionRange CangjieMigrator::PositionRange::Empty()
{
    return {{}, {}};
}

CangjieMigrator::PositionRange::PositionRange(Cangjie::Position start, Cangjie::Position end) : start(start), end(end)
{}

CangjieMigrator::PositionRange::PositionRange(const Cangjie::AST::Node &node)
    : start(node.GetMacroCallPos(node.begin)), end(node.GetMacroCallPos(node.end))
{}

bool CangjieMigrator::PositionRange::IsInclude(const CangjieMigrator::PositionRange &another) const
{
    return start <= another.start && end >= another.end;
}

bool CangjieMigrator::PositionRange::IsInvalid() const
{
    if (start == Cangjie::INVALID_POSITION || end == Cangjie::INVALID_POSITION) {
        return false;
    }
    return end < start || start.fileID != end.fileID;
}

bool CangjieMigrator::PositionRange::IsEmpty() const
{
    return start == end;
}

bool CangjieMigrator::PositionRange::operator==(const CangjieMigrator::PositionRange &other) const
{
    return start == other.start && end == other.end;
}

bool CangjieMigrator::PositionRange::operator!=(const CangjieMigrator::PositionRange &other) const
{
    return !(*this == other);
}

std::string CangjieMigrator::PositionRange::ToString() const
{
    return "[" + start.ToString() + ", " + end.ToString() + "]";
}

namespace CangjieMigrator {

std::ostream &operator<<(std::ostream &os, const PositionRange &range)
{
    os << "[" << range.start << ", " << range.end << "]";
    return os;
}

}  // namespace CangjieMigrator
